package com.hugh.lagoucloudgateway9002.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class SentinelHandlersClass {

    // 整体要求和当时Hystrix一样，这里还需要在形参最后添加BlockException参数，用于接收异常
    // 注意：方法是静态的
    public static Mono<Void> handleException(ServerWebExchange exchange, GatewayFilterChain chain, BlockException blockException) {
        ServerHttpResponse response = exchange.getResponse();

        String data = "SentinelHandlersClass--**--handleException Request be denied!";
        DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
        return response.writeWith(Mono.just(wrap));
    }

    public static Mono<Void> handleError(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpResponse response = exchange.getResponse();
        String data = "SentinelHandlersClass--**--handleError! Request be denied!";
        DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
        return response.writeWith(Mono.just(wrap));
    }

}
